package com.sps.business.service.impl;

import java.util.Date;
import java.util.List;

import com.sps.business.domain.CcStockinfoInfo;
import com.sps.business.domain.CcWarehouserecord;
import com.sps.business.mapper.CcStockinfoInfoMapper;
import com.sps.business.mapper.CcWarehouserecordMapper;
import com.sps.common.exception.ServiceException;
import com.sps.common.utils.DateUtils;
import com.sps.common.utils.NewNumber;
import com.sps.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sps.business.mapper.CcRukuInfoMapper;
import com.sps.business.domain.CcRukuInfo;
import com.sps.business.service.ICcRukuInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 入库任务Service业务层处理
 * 
 * @author 王超
 * @date 2025-03-04
 */
@Service
public class CcRukuInfoServiceImpl implements ICcRukuInfoService 
{
    @Autowired
    private CcRukuInfoMapper ccRukuInfoMapper;

    @Autowired
    private CcStockinfoInfoMapper ccStockinfoInfoMapper;

    @Autowired
    private CcWarehouserecordMapper ccWarehouserecordMapper;

    /**
     * 查询入库任务
     * 
     * @param id 入库任务主键
     * @return 入库任务
     */
    @Override
    public CcRukuInfo selectCcRukuInfoById(Long id)
    {
        return ccRukuInfoMapper.selectCcRukuInfoById(id);
    }

    /**
     * 查询入库任务列表
     * 
     * @param ccRukuInfo 入库任务
     * @return 入库任务
     */
    @Override
    public List<CcRukuInfo> selectCcRukuInfoList(CcRukuInfo ccRukuInfo)
    {
        return ccRukuInfoMapper.selectCcRukuInfoList(ccRukuInfo);
    }

    /**
     * 新增入库任务
     * 
     * @param ccRukuInfo 入库任务
     * @return 结果
     */
    @Override
    public int insertCcRukuInfo(CcRukuInfo ccRukuInfo)
    {
        ccRukuInfo.setCreateTime(DateUtils.getNowDate());
        return ccRukuInfoMapper.insertCcRukuInfo(ccRukuInfo);
    }

    /**
     * 修改入库任务
     * 
     * @param ccRukuInfo 入库任务
     * @return 结果
     */
    @Override
    public int updateCcRukuInfo(CcRukuInfo ccRukuInfo)
    {
        int i = 0;
        //先查询库存中有无记录，有的话更新，没有则新增
        CcStockinfoInfo ccStockinfoInfo = ccStockinfoInfoMapper.selectCcStockinfoInfoByItemId(ccRukuInfo.getItemCode());
        if(ccStockinfoInfo==null){
            //2.向库存表中增加数据
            CcStockinfoInfo insertStockinfoInfo = new CcStockinfoInfo();
            insertStockinfoInfo.setDeptId(103L);
            insertStockinfoInfo.setItemId(ccRukuInfo.getItemCode());//物料号
            insertStockinfoInfo.setItemName(ccRukuInfo.getItemName());//物料名称
            insertStockinfoInfo.setKcNum(ccRukuInfo.getRkNum());//入库数量
            insertStockinfoInfo.setLockNum(0L);//锁帐初始默认为0
            insertStockinfoInfo.setQutlityStauts(0L);//质量状态，默认都合格
            insertStockinfoInfo.setSupplyCode(ccRukuInfo.getSuppCode());
            insertStockinfoInfo.setSupplyName(ccRukuInfo.getSuppName());
            insertStockinfoInfo.setKwCode("sps-001");//库位编号暂定写死
            insertStockinfoInfo.setKwName("sps皮卡");//库位名称 暂定写死
            insertStockinfoInfo.setLableCode(ccRukuInfo.getLableCode());//标签号
            insertStockinfoInfo.setApplianceyCode(ccRukuInfo.getApplianceyCode());//器具位置号
            insertStockinfoInfo.setApplianceyName(ccRukuInfo.getApplianceyName());//器具名称
            insertStockinfoInfo.setSpsArea(ccRukuInfo.getSpsArea());//sps区
            insertStockinfoInfo.setUnit(ccRukuInfo.getUnit());//单位
            insertStockinfoInfo.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());//创建人
            insertStockinfoInfo.setCreateTime(DateUtils.getNowDate());//创建时间
            insertStockinfoInfo.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());//更新人
            insertStockinfoInfo.setUpdateTime(DateUtils.getNowDate());//更新时间
            i = ccStockinfoInfoMapper.insertCcStockinfoInfo(insertStockinfoInfo);
        }else{
            //更新数量
            ccStockinfoInfo.setKcNum(ccStockinfoInfo.getKcNum()+ccRukuInfo.getRkNum());
            ccStockinfoInfo.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());//更新人
            ccStockinfoInfo.setUpdateTime(DateUtils.getNowDate());//更新时间
            i = ccStockinfoInfoMapper.updateCcStockinfoInfo(ccStockinfoInfo);
        }

        //2.向流水表中增加数据
        CcWarehouserecord ccWarehouserecord = new CcWarehouserecord();
        ccWarehouserecord.setItemName(ccRukuInfo.getItemName());
        ccWarehouserecord.setItemId(ccRukuInfo.getItemCode());
        ccWarehouserecord.setBsinssType(0L);
        ccWarehouserecord.setCrNum(ccRukuInfo.getRkNum());
        ccWarehouserecord.setSupplyCode(ccRukuInfo.getSuppCode());
        ccWarehouserecord.setSupplyName(ccRukuInfo.getSuppName());
        ccWarehouserecord.setUnit(ccRukuInfo.getUnit());
        ccWarehouserecord.setKwCode("sps-001");
        ccWarehouserecord.setKwName("sps皮卡");
        ccWarehouserecord.setLableCode(ccRukuInfo.getLableCode());
        ccWarehouserecord.setApplianceyCode(ccRukuInfo.getApplianceyCode());
        ccWarehouserecord.setApplianceyName(ccRukuInfo.getApplianceyName());
        ccWarehouserecord.setSpsArea(ccRukuInfo.getSpsArea());
        ccWarehouserecord.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());//创建人
        ccWarehouserecord.setCreateTime(DateUtils.getNowDate());//创建时间
        ccWarehouserecord.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());//更新人
        ccWarehouserecord.setUpdateTime(DateUtils.getNowDate());//更新时间
        ccWarehouserecordMapper.insertCcWarehouserecord(ccWarehouserecord);
        ccRukuInfoMapper.updateCcRukuInfo(ccRukuInfo);
        return i;
    }


    @Transactional
    @Override
    public int updateCcRukuInfoByPlanCode(CcRukuInfo ccRukuInfo) {
        int i = 0;
        //先查询库存中有无记录，有的话更新，没有则新增
        CcStockinfoInfo ccStockinfoInfo = ccStockinfoInfoMapper.selectCcStockinfoInfoByItemId(ccRukuInfo.getItemCode());
        if(ccStockinfoInfo==null){
            //2.向库存表中增加数据
            CcStockinfoInfo insertStockinfoInfo = new CcStockinfoInfo();
            insertStockinfoInfo.setDeptId(103L);
            insertStockinfoInfo.setItemId(ccRukuInfo.getItemCode());//物料号
            insertStockinfoInfo.setItemName(ccRukuInfo.getItemName());//物料名称
            insertStockinfoInfo.setKcNum(ccRukuInfo.getRkNum());//入库数量
            insertStockinfoInfo.setLockNum(0L);//锁帐初始默认为0
            insertStockinfoInfo.setQutlityStauts(0L);//质量状态，默认都合格
            insertStockinfoInfo.setSupplyCode(ccRukuInfo.getSuppCode());
            insertStockinfoInfo.setSupplyName(ccRukuInfo.getSuppName());
            insertStockinfoInfo.setKwCode("sps-001");//库位编号暂定写死
            insertStockinfoInfo.setKwName("sps皮卡");//库位名称 暂定写死
            insertStockinfoInfo.setLableCode(ccRukuInfo.getLableCode());//标签号
            insertStockinfoInfo.setApplianceyCode(ccRukuInfo.getApplianceyCode());//器具位置号
            insertStockinfoInfo.setApplianceyName(ccRukuInfo.getApplianceyName());//器具名称
            insertStockinfoInfo.setSpsArea(ccRukuInfo.getSpsArea());//sps区
            insertStockinfoInfo.setUnit(ccRukuInfo.getUnit());//单位
            insertStockinfoInfo.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());//创建人
            insertStockinfoInfo.setCreateTime(DateUtils.getNowDate());//创建时间
            insertStockinfoInfo.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());//更新人
            insertStockinfoInfo.setUpdateTime(DateUtils.getNowDate());//更新时间
            i = ccStockinfoInfoMapper.insertCcStockinfoInfo(insertStockinfoInfo);
        }else{
            //更新数量
            ccStockinfoInfo.setKcNum(ccStockinfoInfo.getKcNum()+ccRukuInfo.getRkNum());
            ccStockinfoInfo.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());//更新人
            ccStockinfoInfo.setUpdateTime(DateUtils.getNowDate());//更新时间
            i = ccStockinfoInfoMapper.updateCcStockinfoInfo(ccStockinfoInfo);
        }

        //2.向流水表中增加数据
        CcWarehouserecord ccWarehouserecord = new CcWarehouserecord();
        ccWarehouserecord.setItemName(ccRukuInfo.getItemName());
        ccWarehouserecord.setItemId(ccRukuInfo.getItemCode());
        ccWarehouserecord.setBsinssType(0L);
        ccWarehouserecord.setCrNum(ccRukuInfo.getRkNum());
        ccWarehouserecord.setSupplyCode(ccRukuInfo.getSuppCode());
        ccWarehouserecord.setSupplyName(ccRukuInfo.getSuppName());
        ccWarehouserecord.setUnit(ccRukuInfo.getUnit());
        ccWarehouserecord.setKwCode("sps-001");
        ccWarehouserecord.setKwName("sps皮卡");
        ccWarehouserecord.setLableCode(ccRukuInfo.getLableCode());
        ccWarehouserecord.setApplianceyCode(ccRukuInfo.getApplianceyCode());
        ccWarehouserecord.setApplianceyName(ccRukuInfo.getApplianceyName());
        ccWarehouserecord.setSpsArea(ccRukuInfo.getSpsArea());
        ccWarehouserecord.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());//创建人
        ccWarehouserecord.setCreateTime(DateUtils.getNowDate());//创建时间
        ccWarehouserecord.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());//更新人
        ccWarehouserecord.setUpdateTime(DateUtils.getNowDate());//更新时间
        ccWarehouserecordMapper.insertCcWarehouserecord(ccWarehouserecord);
        ccRukuInfo.setStatus((long)1);//状态更改为已完成
        ccRukuInfoMapper.updateCcRukuInfo(ccRukuInfo);
        return i;
    }

    /*
     * 更改打印次数和状态
     */
    @Override
    public int updateCcRukuInfoRuKuStatus(CcRukuInfo ccRukuInfo) {

        ccRukuInfo.setSn(ccRukuInfo.getSn()+1);
        ccRukuInfo.setRes1("已打印");
        return ccRukuInfoMapper.updateCcRukuInfo(ccRukuInfo);
    }


    /**
     * 批量删除入库任务
     * 
     * @param ids 需要删除的入库任务主键
     * @return 结果
     */
    @Override
    public int deleteCcRukuInfoByIds(Long[] ids)
    {
        return ccRukuInfoMapper.deleteCcRukuInfoByIds(ids);
    }

    /**
     * 删除入库任务信息
     * 
     * @param id 入库任务主键
     * @return 结果
     */
    @Override
    public int deleteCcRukuInfoById(Long id)
    {
        return ccRukuInfoMapper.deleteCcRukuInfoById(id);
    }

    //新增入库任务
    @Override
    public int insertCcRukuInfoList(List<CcRukuInfo> ccRukuInfoList) {
        //任务号
        String number= "BH-"+ NewNumber.createNewNumber(ccRukuInfoMapper.getNewCode(DateUtils.getDate()));
        //生成序列号
        Long maxXlh = ccRukuInfoMapper.selectCcRukuInfoByMaxXlh(DateUtils.dateTime());
        Long resultNum = maxXlh+1;
        Date nowDate = DateUtils.getNowDate();
        String username = SecurityUtils.getUsername();
        for (CcRukuInfo ccRukuInfo : ccRukuInfoList) {
            ccRukuInfo.setXlh(resultNum);
            ccRukuInfo.setRkNum((long) 0);
            ccRukuInfo.setPlanCode(number);
            ccRukuInfo.setSn((long) 0);//打印次数
            ccRukuInfo.setRes1("未打印");
            ccRukuInfo.setCreateUser(username);
            ccRukuInfo.setCreateTime(nowDate);
            ccRukuInfoMapper.insertCcRukuInfo(ccRukuInfo);
        }
        return 1;
    }

    @Override
    public List<CcRukuInfo> selectCcRukuInfoListGroup(CcRukuInfo ccRukuInfo) {
        return ccRukuInfoMapper.selectCcRukuInfoListGroup(ccRukuInfo);
    }

    @Override
    public CcRukuInfo selectCcRukuInfoByCodeAndLable(String planCode, String lableCode) {
        CcRukuInfo ccRukuInfo = ccRukuInfoMapper.selectCcRukuInfoByCodeAndLable(planCode, lableCode);
        if(ccRukuInfo.getStatus() == 1){
            throw new ServiceException("物料:"+ccRukuInfo.getItemCode()+"已经入库");
        }
        return ccRukuInfo;
    }


}
